package com.sinoglobal.ningxia.activity.mico;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.transition.Transition;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.shuyu.gsyvideoplayer.GSYVideoPlayer;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.sinoglobal.networkconnections.ResultCallback;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.beans.programInfoVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;

/**
 * Created by XINDZ on 2017/5/2.
 * 功能:测试GSYVideoPlayer 播放视频
 */

public class GSYVDHActivity extends BaseActivity {
    String url = "http://video.sinosns.cn/fmmgc/shuishangfeixing8.mp4";
    private programInfoVo infoVo;
    String title = "";

    public final static String IMG_TRANSITION = "IMG_TRANSITION";
    public final static String TRANSITION = "TRANSITION";


    OrientationUtils orientationUtils;

    private boolean isTransition = false;

    private Transition transition;
    private StandardGSYVideoPlayer gsyVideoPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setHideTitle(true);
        setContentView(R.layout.activity_gsyvd_h);
        Intent get = getIntent();
        infoVo = (programInfoVo) get.getSerializableExtra(programInfoVo.class.getName());
        if (infoVo == null)
            return;
        url = infoVo.getVideo_url();
        String id = infoVo.getId();
        if (id != null && !id.equals("")) {
            RemoteImpl.getInstance().getNewslookNum(this, id, 2).execute(new ResultCallback(this));
        }
        if (infoVo.getName() != null) {
            title = infoVo.getName();
        }


        gsyVideoPlayer = (StandardGSYVideoPlayer) findViewById(R.id.video_player);

        gsyVideoPlayer.setUp(url, false);
        //设置图像显示比例 4:3 16:9 全屏
        GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_FULL_FXT);

        //增加封面
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(R.drawable.loading_ad_image);
//        gsyVideoPlayer.setThumbImageView(imageView);

        //增加title
        if (title.equals("")) {
            gsyVideoPlayer.getTitleTextView().setVisibility(View.GONE);
        } else {
            gsyVideoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
            gsyVideoPlayer.getTitleTextView().setText(title);
        }


        //设置返回键
        gsyVideoPlayer.getBackButton().setVisibility(View.VISIBLE);

        //设置旋转
        orientationUtils = new OrientationUtils(this, gsyVideoPlayer);
        //是否开启自动旋转
        gsyVideoPlayer.setRotateViewAuto(false);
        //一全屏就锁屏横屏
        gsyVideoPlayer.setLockLand(true);
        //隐藏全屏按键
        gsyVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                orientationUtils.resolveByClick();
                finish();
            }
        });


        //设置返回按键功能
        gsyVideoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        //播放视频
        gsyVideoPlayer.startPlayLogic();
        //设置ActionBar 以及状态栏
//        gsyVideoPlayer.startWindowFullscreen(this, false, true);
        orientationUtils.resolveByClick();
    }


    @Override
    protected void onPause() {
        super.onPause();
        gsyVideoPlayer.onVideoPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoPlayer.releaseAllVideos();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }

    @Override
    public void onBackPressed() {
        if (gsyVideoPlayer != null && gsyVideoPlayer.getCurrentState() == GSYVideoPlayer.CURRENT_STATE_PLAYING) {//如果处在播放状态
            gsyVideoPlayer.onVideoPause();//视频暂停
            return;
        }
        //释放所有
        gsyVideoPlayer.setStandardVideoAllCallBack(null);
        GSYVideoPlayer.releaseAllVideos();
        if (isTransition && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            super.onBackPressed();
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    finish();
                    overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
                }
            }, 500);
        }
    }


}
